{% extends 'index.html' %}

{% block header %}
    <h1 class="page-header">
        host discover page
        <small>Welcome John Doe</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#">Home</a></li>
        <li><a href="#">Dashboard</a></li>
        <li class="active">Data</li>
    </ol>

{% endblock %}


{% block page-inner %}
    {#    <h1> host list page</h1>#}

    <table class="table table-hover">
        <tr>
            <th>ID</th>
            <th>Hostname</th>
            <th>IP</th>
            <th>状态</th>
            <th>是否接受</th>
        </tr>
        {% for key,value in Host_list_dict.items %}

            <tr>
                <td>{{ key }}</td>
                <td>{{ value.0 }}</td>
                <td>{{ value.1 }}</td>
                <td>未接受</td>
                <td><input type="button" value="接受" class="accept"></td>
            </tr>
        {% endfor %}

    </table>
{% endblock %}


{% block customjs %}
    {#    <script#}
    <script type="text/javascript">
        function csrfSafeMethod(method) {
            // these HTTP methods do not require CSRF protection
            return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
        }
        function sameOrigin(url) {
            // test that a given url is a same-origin URL
            // url could be relative or scheme relative or absolute
            var host = document.location.host; // host + port
            var protocol = document.location.protocol;
            var sr_origin = '//' + host;
            var origin = protocol + sr_origin;
            // Allow absolute or scheme relative URLs to same origin
            return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
                (url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
                // or any other URL that isn't scheme relative or absolute i.e relative.
                !(/^(\/\/|http:|https:).*/.test(url));
        }
        function getCookie(name) {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = jQuery.trim(cookies[i]);
                    // Does this cookie string begin with the name we want?
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
                    // Send the token to same-origin, relative URLs only.
                    // Send the token only if the method warrants CSRF protection
                    // Using the CSRFToken value acquired earlier
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });
        csrftoken = getCookie('csrftoken');
        $.ajaxSetup({
            beforeSend: function (xhr, settings) {
                if (!csrfSafeMethod(settings.type) && sameOrigin(settings.url)) {
                    xhr.setRequestHeader("X-CSRFToken", csrftoken);
                }
            }
        });

        $(function () {
            $(".accept").click(function () {
                alert("接受agentkey");
                var Accept_host_name = $(this).parents("tr").find("td").eq(1).html();
                var Accept_host_ip = $(this).parents("tr").find("td").eq(2).html();
{#                alert(Accept_host);#}
{#                console.log($(this).parents("tr").find("td").eq(1).html());#}
                $.ajax({
                    url: "/accept_salt_key/",
                    type: "POST",
                    data: {"Todo": "accept","Minion_host_name":Accept_host_name,"Minion_ipaddress":Accept_host_ip},
                    dataType: "json",
                    success: function (result) {
                        alert(result.rollback);
                        alert(result.result);
                        location.reload();
                    },
                    error: function (result) {
                        alert("接口调用失败");
                    }

                });

            });
        });

    </script>
{% endblock %}
